﻿using Coldairarrow.Entity.Backstage.Valickets;
using Coldairarrow.Util;
using EFCore.Sharding;
using LinqKit;
using Microsoft.EntityFrameworkCore;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Dynamic.Core;
using System.Threading.Tasks;

namespace Coldairarrow.Business.Backstage.Valickets
{
    public class BS_VailTicketBusiness : BaseBusiness<BS_VailTicket>, IBS_VailTicketBusiness, ITransientDependency
    {
        public BS_VailTicketBusiness(IDbAccessor db)
            : base(db)
        {
        }

        #region 外部接口

        public async Task<PageResult<BS_VailTicket>> GetDataListAsync(PageInput<SearchDTO> input)
        {
            var q = GetIQueryable();
            var where = LinqHelper.True<BS_VailTicket>();
            var search = input.Search;

            if (!search.Keyword.IsNullOrEmpty())
            {
                where = where.And(x => x.Ticket_Title.Equals(search.Keyword) || x.Ticket_Abstract.Equals(search.Keyword));
            }

            if (search.Type.HasValue)
            {
                where = where.And(x => x.Ticket_Type == search.Type);
            }

            if (search.State.HasValue)
            {
                where = where.And(x => x.State == search.State);
            }

            return await q.Where(where).GetPageResultAsync(input);
        }

        public List<BS_VailTicket> GetTicketList()
        {
            var sList = GetIQueryable().Where(x => x.State == true && x.Ticket_Type == 0).OrderByDescending(x => x.Ticket_Amount).ToList();

            return sList;
        }

        public async Task<BS_VailTicket> GetTheDataAsync(string id)
        {
            return await GetEntityAsync(id);
        }

        public async Task AddDataAsync(BS_VailTicket data)
        {
            if (!data.Ticket_VailDay.HasValue && !data.Ticket_EndTime.HasValue)
            {
                data.Ticket_VailDay = 3;
            }

            if (data.Ticket_Type == 1 && !data.Ticket_UseMoney.HasValue)
            {
                throw new BusException("满减优惠券必须填满减额！", -2);
            }

            if (data.Ticket_UseMoney.HasValue && data.Ticket_Amount >= data.Ticket_UseMoney)
            {
                throw new BusException("代金券最小消费金额必须大于代金券额度，新增失败！", -4);
            }

            var aCount = GetIQueryable().Where(x => x.Ticket_Amount == data.Ticket_Amount && x.Ticket_VailDay == data.Ticket_VailDay && x.Ticket_EndTime == data.Ticket_EndTime).Count();
            if (aCount > 0)
            {
                throw new BusException("该代金券已存在，不能重复创建！",-1);
            }

            if (data.Ticket_Type == 1)
            {
                data.Ticket_ChangePoint = 999999;
            }

            await InsertAsync(data);
        }

        public async Task UpdateDataAsync(BS_VailTicket data)
        {
            if (!data.Ticket_VailDay.HasValue && !data.Ticket_EndTime.HasValue)
            {
                data.Ticket_VailDay = 3;
            }

            if (data.Ticket_Type == 1 && !data.Ticket_UseMoney.HasValue)
            {
                throw new BusException("满减优惠券必须填满减额！", -2);
            }

            if (data.Ticket_UseMoney.HasValue && data.Ticket_Amount >= data.Ticket_UseMoney)
            {
                throw new BusException("代金券最小消费金额必须大于代金券额度，修改失败！", -4);
            }

            var aCount = GetIQueryable().Where(x => !x.Id.Equals(data.Id) &&  x.Ticket_Amount == data.Ticket_Amount && x.Ticket_VailDay == data.Ticket_VailDay && x.Ticket_EndTime == data.Ticket_EndTime).Count();
            if (aCount > 0)
            {
                throw new BusException("该代金券与其他代金券重复，修改失败！",-3);
            }

            if (data.Ticket_Type == 1)
            {
                data.Ticket_ChangePoint = 999999;
            }

            await UpdateAsync(data);
        }

        public async Task ChangeState(string id)
        {
            var data = await GetEntityAsync(id);
           if(!data.State.HasValue)
           {
                data.State = true;
           }
           else if(data.State == false)
           {
                data.State = true;
           }
           else if (data.State == true)
           {
                data.State = false;
           }
        }

        public async Task DeleteDataAsync(List<string> ids)
        {
            await DeleteAsync(ids);
        }

        #endregion

        #region 私有成员

        #endregion
    }
}